package com.example.led;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LEDActivity extends Activity {
	
	private NotificationManager mNotificationManager;
	private static final int HELLO_ID = 1;
	private static final int LED_NOTIFICATION_ID = 2;
	private Notification mNotification;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button onButton = (Button) findViewById(R.id.buttonOn);
        Button offButton = (Button) findViewById(R.id.buttonOff);
        Button cancelButton = (Button) findViewById(R.id.buttonCancel);
        
        onButton.setOnClickListener(btn_listener);
        offButton.setOnClickListener(btn_listener);
        cancelButton.setOnClickListener(btn_listener);
        
        // Get the notification manager
		String ns = Context.NOTIFICATION_SERVICE;
		mNotificationManager = (NotificationManager) getSystemService(ns);
		        
        //Instantiate the notification      
		int icon = R.drawable.ic_launcher;        // icon from resources
		CharSequence tickerText = "Hello";              // ticker-text
		long when = System.currentTimeMillis();         // notification time
		mNotification = new Notification(icon, tickerText, when);
		
		//Define the notification's message and PendingIntent
		Context context = getApplicationContext();      // application Context
		CharSequence contentTitle = "My notification";  // message title
		CharSequence contentText = "Hello World!";      // message text
		Intent notificationIntent = new Intent(this, LEDActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
		
		mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		
		// Cancel it if the user selects it
		mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
		
		// Use the default light settings
		//mNotification.defaults |= Notification.DEFAULT_LIGHTS;
		
		//Green, on for 300ms, off for 1s.
		mNotification.ledARGB = 0xff00ff00;
		mNotification.ledOnMS = 300;
		mNotification.ledOffMS = 1000;
		mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
		mNotification.flags |= Notification.FLAG_INSISTENT;
    }
    
    private OnClickListener btn_listener = new OnClickListener() {
    	public void onClick(View v) {
    		Button b = (Button) v;
    		if(b.getId() == R.id.buttonOn) {
    			mNotificationManager.notify(HELLO_ID, mNotification);
    			//RedFlashLight();
    		} else if (b.getId() == R.id.buttonOff) {    			
    			mNotificationManager.cancel(HELLO_ID);
    		} else if (b.getId() == R.id.buttonCancel) {
    			//Only cancels notifications raised from this app
    			mNotificationManager.cancelAll();
    		}
    	}
    };
    
}